import HRDCCustomerApi from '@/api/HRDCCustomer.api.js'
import dataApi from '@/api/data.js'
import visibvleTreeList from '../HRDC-common/visibleTreeList.vue'
import moment from 'moment'
export default {
  data () {
    return {
      dolderNames: '',
      salesIds: [],
      treeData: [],
      customerId: '', // 客户id
      contact_arr: [], // 联系人列表
      customerBusinesstype_arr: [], // 行业
      sizeArr: [], // 公司规模
      characterArr: [], // 企业性质
      customerSourceArr: [], // 客户来源
      communicationtype: [], // 沟通方式
      communicationTotal: 0,
      communicationList: [] // 沟通记录列表
    }
  },
  components: {
    visibvleTreeList
  },
  computed: {
    tipIsShow () {
      if (this.dolderNames) {
        return true
      } else {
        return false
      }
    }
  },
  created () {
    this.getEnum()
    this.getVisibleTreeList()
    this.getCustomerSource()
  },
  methods: {
    // 跳往详情
    gotoDetail (id) {
      this.$router.push({ name: 'h-c-detail', query: { id: id } })
    },
    // 设置表头
    getTableColumns (arr) {
      const tableColumns = {
        selection: {
          type: 'selection',
          width: 60,
          align: 'center',
          fixed: 'left'
        },
        createTime: {
          title: '新建日期',
          minWidth: 120,
          align: 'center',
          key: 'createTime',
          render: (h, params) => {
            let row = params.row
            let times = row.createTime.slice(0, 10).replace(/T/g, ' ')
            return h('span', times)
          }
        },
        throwTime: {
          title: '流入公海时间',
          minWidth: 160,
          align: 'center',
          key: 'throwTime',
          render: (h, params) => {
            const text = params.row.throwTime ? moment(params.row.throwTime).format('YYYY-MM-DD HH:mm') : ''
            return h('span', text)
          }
        },
        source: {
          title: '客户来源',
          minWidth: 120,
          align: 'center',
          key: 'source'
        },
        tagName: {
          title: '客户标签',
          minWidth: 120,
          align: 'center',
          key: 'tagName'
        },
        signedTypeName: {
          title: '客户类型',
          minWidth: 120,
          align: 'center',
          key: 'signedTypeName'
        },
        // signedTypeName: {
        //   title: '客户分类',
        //   minWidth: 170,
        //   align: 'center',
        //   key: 'signedTypeName'
        // },
        name: {
          title: '客户名称',
          minWidth: 150,
          align: 'center',
          slot: 'name'
        },
        budgetDetail: {
          title: '今年预算',
          minWidth: 120,
          align: 'center',
          key: 'budgetDetail'
        },
        plan: {
          title: '今年计划',
          minWidth: 120,
          align: 'center',
          key: 'plan'
        },
        historySuppliers: {
          title: '过往供应商',
          minWidth: 120,
          tooltip: true,
          align: 'center',
          key: 'historySuppliers'
        },
        historyProjects: {
          title: '过往项目',
          minWidth: 120,
          tooltip: true,
          align: 'center',
          key: 'historyProjects'
        },
        characterName: {
          title: '企业性质',
          minWidth: 120,
          tooltip: true,
          align: 'center',
          key: 'characterName'
        },
        businessTypeName: {
          title: '所属行业',
          minWidth: 120,
          tooltip: true,
          align: 'center',
          key: 'businessTypeName'
        },
        registerCapital: {
          title: '注册资本',
          minWidth: 120,
          tooltip: true,
          align: 'center',
          key: 'registerCapital'
        },
        size: {
          title: '企业规模',
          minWidth: 140,
          align: 'center',
          render: (h, params) => {
            let row = params.row
            let str = ''
            let arr = this.sizeArr
            let text = arr.find((item) => {
              return item.key === row.size.toString()
            })
            if (text) {
              str = text.value
            } else {
              str = '未填写'
            }
            return h('span', str)
          }

        },
        address: {
          title: '客户地址',
          minWidth: 150,
          tooltip: true,
          align: 'center',
          key: 'address'
        },
        webSite: {
          title: '客户网站',
          minWidth: 150,
          tooltip: true,
          align: 'center',
          key: 'webSite'
        },
        phone: {
          title: '客户公司电话',
          minWidth: 150,
          align: 'center',
          key: 'phone'
        },
        holders: {
          title: '所属销售',
          minWidth: 100,
          align: 'center',
          key: 'holders'
        },
        createrName: {
          title: '创建人',
          minWidth: 100,
          align: 'center',
          key: 'createrName'
        },
        contact: {
          title: '客户联系人',
          minWidth: 110,
          align: 'center',
          slot: 'contact'
        },
        communicate: {
          title: '沟通记录',
          minWidth: 170,
          align: 'center',
          slot: 'communicate',
          fixed: 'right'
        },
        // updateSign: {
        //   title: 'KA客户',
        //   minWidth: 100,
        //   align: 'center',
        //   slot: 'updateSign',
        //   fixed: 'left'
        // },
        action: {
          title: '操作',
          minWidth: 180,
          align: 'center',
          slot: 'action',
          fixed: 'right'
        },
        action_2: {
          title: '操作',
          minWidth: 240,
          align: 'center',
          slot: 'action_2',
          fixed: 'right'
        }
      }
      let data = []

      arr.forEach(col => data.push(tableColumns[col]))

      return data
    },
    // 添加沟通记录
    addCommunication (id) {
      this.customerId = id
      this.getContactList()
      // this.$refs.createCommunication.creatOpen()
    },
    checkCommunication (id) {
      this.customerId = id
      this.getCommunicationList()
      this.$refs.checkCommunication.creatOpen()
    },
    // 获取联系人列表
    getContactList () {
      HRDCCustomerApi.ContacterList({
        customerId: this.customerId,
        PageIndex: 0,
        PageSize: 100
      }).then(res => {
        if (res.data.success) {
          this.contact_arr = res.data.data
          this.$refs.createCommunication.creatOpen()
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    // 获取当前角色可见的销售人员
    getVisibleTreeList () {
      HRDCCustomerApi.GetVisibleTreeList().then(res => {
        console.log(res)
        this.treeData = res.data.data
      })
    },
    chooseSaler () {
      this.$refs.treeModal.show()
    },
    salersClear () {
      console.log(22222)
      this.salesIds = []
      this.getVisibleTreeList()
    },
    getSalerId (val) {
      console.log(val)
      let idArr = []
      let nameArr = []
      val.forEach(item => {
        if (item.type === "4") {
          idArr.push(item.id)
          nameArr.push(item.title)
        }
      })
      this.salesIds = idArr
      this.dolderNames = nameArr.join(',')
    },
    // 客户来源下拉数据
    getCustomerSource () {
      HRDCCustomerApi.getCustomerSource().then(res => {
        this.customerSourceArr = res.data.data
      })
    },
    // 获取沟通记录列表
    getCommunicationList () {
      HRDCCustomerApi.CommunicationList({
        customerId: this.customerId,
        PageIndex: 0,
        PageSize: 100
      }).then(res => {
        if (res.data.success) {
          this.communicationList = res.data.data
          this.communicationTotal = res.data.totalRows
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    getEnum () {
      let groupNames = 'customer.businesstype,sales.communicationtype,customer.size,customer.character,sales.customersignedtype'
      dataApi.getEnum({
        groupName: groupNames
      }).then((res) => {
        this.customerBusinesstype_arr = this.getItems(res.data, 'customer.businesstype')
        this.communicationtype = this.getItems(res.data, 'sales.communicationtype')
        this.sizeArr = this.getItems(res.data, 'customer.size')
        this.characterArr = this.getItems(res.data, 'customer.character')
        this.customersignedtype = this.getItems(res.data, 'sales.customersignedtype')
      })
      // Common.getEnum(groupName, this.arrListDown)
    },
    getItems (arr, key) {
      let object = arr.find((item) => {
        return item.group === key
      })
      return object.items
    }
  }
}
